package com.xzit.headline.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.j256.ormlite.dao.Dao;
import com.xzit.headline.R;
import com.xzit.headline.bean.NewListData;
import com.xzit.headline.db.MyDbHelper;

import java.sql.SQLException;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


//1.布局
//2.加载布局
public class DetailFragment extends BaseFragment {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.font)
    ImageView font;
    @BindView(R.id.share)
    ImageView share;
    @BindView(R.id.favourite)
    ImageView favourite;
    @BindView(R.id.progress)
    ProgressBar progress;
    @BindView(R.id.webView)
    WebView webView;

    //定义成员变量与构造方法
    private NewListData.DataBean.NewsBean bean;

    public DetailFragment(NewListData.DataBean.NewsBean bean) {
        System.out.println("详情页获取到的数据:" + bean.title);
        this.bean = bean;
    }

    public DetailFragment() {
    }

    @Override
    public View getMyView() {
        View view = View.inflate(getContext(), R.layout.fragment_detail, null);
        ButterKnife.bind(this, view);

        initDao();
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //3.加载网页数据
        loadWebView();

    }

    @Override
    public void onResume() {
        super.onResume();

        //查询当前新闻是否存在,如果存在,显示已收藏
        //否则显示没收藏
        try {
            NewListData.DataBean.NewsBean newsBean = dao.queryForId(bean.id);
            if (newsBean == null) {
                isSaved = false;
                favourite.setImageResource(R.mipmap.favourite);
            }else {
                isSaved = true;
                favourite.setImageResource(R.mipmap.favourite_selected);
            }
        } catch (SQLException e) {
            e.printStackTrace();

        }
    }


    /**
     * 网页请求方法
     */
    private void loadWebView() {
        //webView发请求加载网页数据
        //webView.loadUrl(BASEURL + "10007/724D6A55496A11726628.html");
        webView.loadUrl(HOST + bean.url);
        webView.setWebViewClient(new WebViewClient());
        webView.setWebChromeClient(new MyWebChromeClient());//可以获取加载进度

        webView.getSettings().setJavaScriptEnabled(true);//设置js可用

    }

    /**
     * 4.重写onProgressChanged方法,进度条的设置
     */
    class MyWebChromeClient extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            //System.out.println("进度" + newProgress);
            if (newProgress != 100) {
                progress.setMax(100);
                progress.setProgress(newProgress);
                progress.setVisibility(View.VISIBLE);
            } else {
                progress.setVisibility(View.INVISIBLE);
            }
        }
    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.back, R.id.font, R.id.share, R.id.favourite})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                onBackClick();
                break;
            case R.id.font:
                onFontClick();
                break;
            case R.id.share:
                break;
            case R.id.favourite:
                onSaveClick();
                break;
        }
    }

    /**
     * 收藏键设置2
     */
    private Dao<NewListData.DataBean.NewsBean, Integer> dao;

    private void initDao() {
        MyDbHelper helper = new MyDbHelper(getContext());
        try {
            dao = helper.getDao(NewListData.DataBean.NewsBean.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 收藏键设置1
     */
    private boolean isSaved = false;

    private void onSaveClick() {
        if (isSaved) {
            isSaved = false;//取消收藏
            favourite.setImageResource(R.mipmap.favourite);
            Toast.makeText(getContext(), "取消收藏", Toast.LENGTH_SHORT).show();
            try {
                //删除数据库中的新闻
                dao.deleteById(bean.id);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } else {
            isSaved = true;//收藏成功
            favourite.setImageResource(R.mipmap.favourite_selected);
            Toast.makeText(getContext(), "收藏成功", Toast.LENGTH_SHORT).show();
            try {
                dao.create(bean);//保存到数据库
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        try {
            List<NewListData.DataBean.NewsBean> list = dao.queryForAll();
            for (NewListData.DataBean.NewsBean item : list) {
                System.out.println(item);
            }
            System.out.println("遍历结束");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 字体设置键
     */
    private String[] levels = {"小", "中", "大", "特大"};
    private int choiceItem = 1;

    private void onFontClick() {
        //5.包含webView的设置参数
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());

        //设置标题
        builder.setTitle("标题");

        //设置选项
        builder.setSingleChoiceItems(levels, choiceItem, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                System.out.println("which" + which);
                choiceItem = which;
            }
        });//单选  参数一:选项数组  参数二:默认选中  参数三:处理时间

        //设置确认按钮
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (choiceItem == 0) {
                    webView.getSettings().setTextSize(WebSettings.TextSize.SMALLER);
                } else if (choiceItem == 1) {
                    webView.getSettings().setTextSize(WebSettings.TextSize.NORMAL);
                } else if (choiceItem == 2) {
                    webView.getSettings().setTextSize(WebSettings.TextSize.LARGER);
                } else if (choiceItem == 3) {
                    webView.getSettings().setTextSize(WebSettings.TextSize.LARGEST);
                }
                dialog.dismiss();//关闭
            }
        });//参数一:按钮的名字  参数二:事件

        //设置取消按钮
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();//关闭
            }
        });//参数一:按钮的名字  参数二:事件

        //应用选项界面
        AlertDialog alertDialog = builder.create();
        alertDialog.show();//打开

    }

    /**
     * 返回键
     */
    private void onBackClick() {
        getActivity().finish();
    }
}
